package cn.lh.system.service.impl;

import cn.lh.basic.util.JsonResult;
import cn.lh.system.domain.Menu;
import cn.lh.system.mapper.MenuMapper;
import cn.lh.system.service.IMenuService;
import cn.lh.basic.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.SplitMapUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getMenuTree() {
        List<Menu> menus = menuMapper.loadAll();
        Map<Long, Menu> map = menus.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));
        List<Menu> menuTree = new ArrayList<>();
        for (Menu m: menus) {
            Long parentId = m.getParentId();
            if (parentId == null) {
                menuTree.add(m);
            } else {
                Menu parentMenu = map.get(parentId);
                parentMenu.getChildren().add(m);
            }
        }
        return menuTree;
    }
}
